package com.moon;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.util.ImageLoaderHelper;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;


public class FragmentDynamic extends RelativeLayout {
	View mView = null;
	public MyAdapter mAdapter;
	ArrayList<JSONObject> mArrayList = new ArrayList<JSONObject>();
	Context mContext = null;
	Activity mActivity = null;
	int mType;
	boolean needShowWait = false;
	String warningStr = null;
	
	public FragmentDynamic(Context context) {
		super(context);
	}
	
	public FragmentDynamic(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@SuppressWarnings("deprecation")
	public FragmentDynamic(Context context,int type) {
		super(context);

		mType = type;
		mContext = context;
		mActivity = (Activity)context;
		
    	LayoutInflater mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	mView = mInflater.inflate(R.layout.fragment_list, null);
    	
    	RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
		this.addView(mView, param);

		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) mView.findViewById(R.id.listview);
		ptrlvHeadlineNews.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.BOTH);
		ptrlvHeadlineNews.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() 
		{
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) 
			{
				if(refreshView.getCurrentMode() == PullToRefreshBase.Mode.PULL_FROM_START)
				{
					sendRequest(false);
				}
				else
				{
					sendRequest(true);
				}
			}
		});
		
		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		listview.setVerticalScrollBarEnabled(false);
		mAdapter = new MyAdapter();
		listview.setOnScrollListener(new OnScrollListener(){
			@Override
			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				((MainPageActivity)mActivity).setDotInVisible(3);
			}

			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				
			}
		});
		listview.setAdapter(mAdapter);
		
		if(mArrayList.size() == 0 && mType == 4)
		{
			initData();
		}
	}

	public void initData()
	{
		if(mAdapter == null) return;
		mAdapter.notifyDataSetChanged();
		
		if(mArrayList.size() == 0) needShowWait = true;
		sendRequest(false);
	}
	
	class MyAdapter extends BaseAdapter implements OnClickListener{
		public int getCount() {
			return mArrayList.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_main, null);
				
				foodHold.icon = (ImageView)convertView.findViewById(R.id.icon);
				foodHold.date = (TextView)convertView.findViewById(R.id.date);
				foodHold.time = (TextView)convertView.findViewById(R.id.time);
				foodHold.name = (TextView)convertView.findViewById(R.id.name);
				foodHold.classname = (TextView)convertView.findViewById(R.id.classname);
				foodHold.content = (TextView)convertView.findViewById(R.id.content);
				foodHold.sign = (TextView)convertView.findViewById(R.id.sign);

				foodHold.img0 = (ImageView)convertView.findViewById(R.id.img0);
				foodHold.img1 = (ImageView)convertView.findViewById(R.id.img1);
				foodHold.img2 = (ImageView)convertView.findViewById(R.id.img2);
				foodHold.img3 = (ImageView)convertView.findViewById(R.id.img3);
				foodHold.img4 = (ImageView)convertView.findViewById(R.id.img4);
				foodHold.img5 = (ImageView)convertView.findViewById(R.id.img5);
				foodHold.img6 = (ImageView)convertView.findViewById(R.id.img6);
				foodHold.img7 = (ImageView)convertView.findViewById(R.id.img7);
				foodHold.img8 = (ImageView)convertView.findViewById(R.id.img8);
				foodHold.arrow = (ImageView)convertView.findViewById(R.id.arrow);

				foodHold.comment0 = (TextView)convertView.findViewById(R.id.comment0);
				foodHold.comment1 = (TextView)convertView.findViewById(R.id.comment1);
				foodHold.comment2 = (TextView)convertView.findViewById(R.id.comment2);

				foodHold.praise = (TextView)convertView.findViewById(R.id.praise);
				foodHold.comment = (TextView)convertView.findViewById(R.id.comment);
				foodHold.line = (View)convertView.findViewById(R.id.line);
				
				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}
			
			JSONObject item = mArrayList.get(position);
			foodHold.pos = position;
			
			if(mType != 5)
			{
				String path = item.getString("avatar_path");
				if(path != null && path.length() != 0) path = "http://120.27.129.233/ylbb/" + path;
				else path = "";
				if(item.getInteger("is_teacher") == 0)
					ImageLoaderHelper.displayImage(R.drawable.ic_family, foodHold.icon,path);
				else 
					ImageLoaderHelper.displayImage(R.drawable.ic_teacher, foodHold.icon,path);
				
				
				foodHold.icon.setOnClickListener(this);
				foodHold.icon.setTag(Integer.valueOf(position));
			
				String name = item.getString("creator_name");
				if(item.getString("creator_alias") != null && item.getString("creator_alias").length() > 0) name = item.getString("creator_alias");
				if(item.getString("creator").equals(PreferencesUtils.getString(mActivity, "userId"))) name = "我";
				foodHold.name.setText(name);

				foodHold.content.setText(item.getString("content"));
				
				name = item.getString("class_name");
				if(item.getString("class_name_alias") != null && item.getString("class_name_alias").length() > 0) name = item.getString("class_name_alias");
				
				foodHold.classname.setText(name);
				foodHold.time.setText(Utils.getDateStr(item.getString("created_at")));
				
//				foodHold.date.setVisibility(View.VISIBLE);
				foodHold.date.setText(getType(item.getIntValue("type")));
			}
			else
			{
				foodHold.icon.setOnClickListener(null);
				foodHold.icon.setImageResource(R.drawable.ic_launcher);
				foodHold.name.setText("月亮巴巴");
				Date date = Utils.ConverToDate(item.getString("dateline"), "yyyy-MM-dd HH:mm:ss");
				if(item.getString("content") != null)foodHold.content.setText(item.getString("content"));
				foodHold.classname.setText(item.getString("class_name"));
				foodHold.time.setText(Utils.ConverToString(date, "yyyy-MM-dd"));

				foodHold.date.setVisibility(View.GONE);
				int isinout = item.getIntValue("isinout");
				if(isinout == 1)
				{
//					foodHold.date.setVisibility(View.VISIBLE);
					foodHold.date.setText("入校");
				}
				else if(isinout == 2)
				{
//					foodHold.date.setVisibility(View.VISIBLE);
					foodHold.date.setText("离校");
				}
			}
			
			foodHold.img0.setVisibility(View.GONE);
			foodHold.img1.setVisibility(View.GONE);
			foodHold.img2.setVisibility(View.GONE);
			foodHold.img3.setVisibility(View.GONE);
			foodHold.img4.setVisibility(View.GONE);
			foodHold.img5.setVisibility(View.GONE);
			foodHold.img6.setVisibility(View.GONE);
			foodHold.img7.setVisibility(View.GONE);
			foodHold.img8.setVisibility(View.GONE);
			
			foodHold.comment0.setVisibility(View.GONE);
			foodHold.comment1.setVisibility(View.GONE);
			foodHold.comment2.setVisibility(View.GONE);

			foodHold.praise.setVisibility(View.GONE);
			foodHold.comment.setVisibility(View.GONE);
			foodHold.line.setVisibility(View.GONE);
			
			foodHold.comment0.setOnClickListener(this);
			foodHold.comment1.setOnClickListener(this);
			foodHold.comment2.setOnClickListener(this);
//			18073179777
			foodHold.comment0.setTag(Integer.valueOf(position));
			foodHold.comment1.setTag(Integer.valueOf(position));
			foodHold.comment2.setTag(Integer.valueOf(position));
			
			foodHold.sign.setVisibility(View.GONE);
			if(item.getInteger("is_teacher") != null && item.getInteger("is_teacher") == 1)
				foodHold.sign.setVisibility(View.VISIBLE);
			
			if(item.getInteger("receipt") == 1 && item.getInteger("is_feedbacked") == 0) foodHold.comment2.setVisibility(View.VISIBLE);
			if(mType != 5)
			{
				if(item.getInteger("type") == 4)
				{
					if(item.getInteger("is_praised") == 0) foodHold.comment0.setVisibility(View.VISIBLE);
					foodHold.comment1.setVisibility(View.VISIBLE);
				}
				
				String img = item.getString("attach_list");
				if(img != null && img.length() > 0)
				{
					String[] imgs = img.split(",");
					int count = imgs.length;
					for(int i = 0;i < count; i++)
					{
						ImageView pImg = null;
						if(i == 0) pImg = foodHold.img0;
						else if(i == 1) pImg = foodHold.img1;
						else if(i == 2) pImg = foodHold.img2;
						else if(i == 3) pImg = foodHold.img3;
						else if(i == 4) pImg = foodHold.img4;
						else if(i == 5) pImg = foodHold.img5;
						else if(i == 6) pImg = foodHold.img6;
						else if(i == 7) pImg = foodHold.img7;
						else if(i == 8) pImg = foodHold.img8;
						
						pImg.setVisibility(View.VISIBLE);
						ImageLoaderHelper.displayImage(0, pImg, "http://120.27.129.233/ylbb/api/" + imgs[i] + "&width=160&height=160");
						pImg.setOnClickListener(this);
						pImg.setTag(foodHold);
					}
				}
				
				foodHold.arrow.setVisibility(View.GONE);
				
				String praiseStr = item.getString("praiseStr");
				String commentStr = item.getString("commentStr");
				
				if(praiseStr.length() > 0 || commentStr.length() > 0)
				{
					foodHold.arrow.setVisibility(View.VISIBLE);
				}
				
				if(praiseStr.length() > 0 && commentStr.length() > 0) foodHold.line.setVisibility(View.VISIBLE);
				if(praiseStr.length() > 0)
				{
					foodHold.praise.setVisibility(View.VISIBLE);
					foodHold.praise.setText(praiseStr);
				}
				
				if(commentStr.length() > 0)
				{
					foodHold.comment.setVisibility(View.VISIBLE);
					foodHold.comment.setText(commentStr);
				}
			}
			
			return convertView;
		}

		public final class FoodHolder {
			public ImageView icon;
			public TextView date;
			public TextView time;
			public TextView name;
			public TextView classname;
			public TextView content;
			public TextView sign;
			
			public ImageView img0;
			public ImageView img1;
			public ImageView img2;
			public ImageView img3;
			public ImageView img4;
			public ImageView img5;
			public ImageView img6;
			public ImageView img7;
			public ImageView img8;
			public ImageView arrow;
			
			public TextView comment0;
			public TextView comment1;
			public TextView comment2;
			
			public TextView praise;
			public View line;
			public TextView comment;
			
			public int pos;
		}
		
		String getType(int type)
		{
			switch(type)
			{
			case 1:return "  作业";
			case 2:case 3:return "  通知";
			case 4:return "  动态";
			case 5:return "  进校/离校";
			}
			
			return "";
		}

		@Override
		public void onClick(View arg0) {
			int viewId = arg0.getId();
			switch(viewId)
			{
			case R.id.comment0:
				sendPraiseRequest((Integer)arg0.getTag());
				break;

			case R.id.comment1:
				showQuerySearch((Integer)arg0.getTag());
				break;
			
			case R.id.comment2:
				sendFeedbackRequest((Integer)arg0.getTag());
				break;
				
			case R.id.img0:
			case R.id.img1:
			case R.id.img2:
			case R.id.img3:
			case R.id.img4:
			case R.id.img5:
			case R.id.img6:
			case R.id.img7:
			case R.id.img8:
				showImage((FoodHolder)arg0.getTag(),viewId);
				break;
				
			case R.id.icon:
			{
				JSONObject item = mArrayList.get((Integer)arg0.getTag());
				Intent intent = new Intent(mActivity,PersonInfoActivity.class);
				intent.putExtra("name", item.getString("name"));
				intent.putExtra("userId",item.getString("creator"));
				intent.putExtra("flag", 0);
				mContext.startActivity(intent);
			}
				break;
			}
		}
		
		
		void showImage(FoodHolder foodHolder,int viewId)
		{
			((DemoApp)mContext.getApplicationContext()).cleanBitmap();
			
			JSONObject item = mArrayList.get(foodHolder.pos);
			int index = viewId - R.id.img0;

			Intent intent = new Intent(mActivity,ImageShowActivity.class);
			String[] array = item.getString("attach_list").split(",");
			for(int i = 0; i < array.length; i++)
			{
				ImageView imageview = null;
				if(0 == i) imageview = foodHolder.img0;
				if(1 == i) imageview = foodHolder.img1;
				if(2 == i) imageview = foodHolder.img2;
				if(3 == i) imageview = foodHolder.img3;
				if(4 == i) imageview = foodHolder.img4;
				if(5 == i) imageview = foodHolder.img5;
				if(6 == i) imageview = foodHolder.img6;
				if(7 == i) imageview = foodHolder.img7;
				if(8 == i) imageview = foodHolder.img8;
				imageview.setDrawingCacheEnabled(true);
				Bitmap bitmap = imageview.getDrawingCache();
				
				((DemoApp)mContext.getApplicationContext()).mBitmapArray.add(bitmap);
			}
			
			intent.putExtra("imgs", item.getString("attach_list"));
			intent.putExtra("index", index);
			mContext.startActivity(intent);
		}
	}
	
	private void sendCommentkRequest(int postId,final String text) {
		final JSONObject item = mArrayList.get(postId);
		
		MgqDataHandler loginHandler = new MgqDataHandler(mActivity, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					
					String commentStr = item.getString("commentStr");
					if(commentStr.length() > 0) commentStr += "\n";
					commentStr += "我: " + text;
					item.put("commentStr", commentStr);
					mAdapter.notifyDataSetChanged();
					needShowWait = true;
					sendRequest(false);
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(mActivity, "userId"));
		params.put("post_id",  item.getIntValue("id"));
		params.put("comment", text);
		MgqRestClient.get("send_post_comment", params, loginHandler);
	}
	
	private void sendFeedbackRequest(int postId) {
		final JSONObject item = mArrayList.get(postId);
		
		MgqDataHandler loginHandler = new MgqDataHandler(mActivity, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					needShowWait = true;
					sendRequest(false);
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(mActivity, "userId"));
		params.put("post_id", item.getIntValue("id"));
		MgqRestClient.get("send_post_feedback", params, loginHandler);
	}
	
	private void sendPraiseRequest(int postId) {
		final JSONObject item = mArrayList.get(postId);
		
		MgqDataHandler loginHandler = new MgqDataHandler(mActivity, false, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
			}
		};
		
		String praiseStr = item.getString("praiseStr");
		if(praiseStr.length() > 0) praiseStr += "，";
		praiseStr += "我";
		item.put("is_praised", 1);
		item.put("praiseStr", praiseStr);
		mAdapter.notifyDataSetChanged();
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(mActivity, "userId"));
		params.put("post_id", item.getIntValue("id"));
		MgqRestClient.get("send_post_praise", params, loginHandler);
	}
	
	public void showImagePicker()
	{
		final Dialog mDialog = new Dialog(mActivity, R.style.Dialog_Fullscreen);
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				if(R.id.dialog_layout == arg0.getId())
				{
					mDialog.dismiss();
				}
				else if(R.id.confirm == arg0.getId())
				{
					if(((DemoApp)mActivity.getApplicationContext()).mClassesSelected.size() == 0)
					{
						ToastUtil.showToastWithAlertPic("至少需要选择一个班级");
						return;
					}
					
					mDialog.dismiss();
					
					mArrayList.removeAll(mArrayList);
					mAdapter.notifyDataSetChanged();

					needShowWait = true;
					sendRequest(false);
				}
				else
				{
					Integer id = (Integer)arg0.getTag();
					if(isClass(id))
					{
						((TextView)arg0).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win, 0, 0, 0);
						removeClassId(id);
					}
					else
					{
						((TextView)arg0).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win_focus, 0, 0, 0);
						((DemoApp)mActivity.getApplicationContext()).mClassesSelected.add(id);
					}
				}
			}
		};

		mDialog.setContentView(R.layout.dialog_class);
		
		JSONArray classes = JSON.parseArray(PreferencesUtils.getString(mActivity, "classes"));
		int size = classes.size();
		for(int i = 0; i < size; i++)
		{
			JSONObject item = classes.getJSONObject(i);
			
			mDialog.findViewById(R.id.filter0 + i).setOnClickListener(listen);
			mDialog.findViewById(R.id.filter0 + i).setVisibility(View.VISIBLE);
			mDialog.findViewById(R.id.filter0 + i).setTag(item.getInteger("id"));

			if(i == 0) mDialog.findViewById(R.id.line0).setVisibility(View.VISIBLE);
			if(i == 1) mDialog.findViewById(R.id.line1).setVisibility(View.VISIBLE);
			if(i == 2) mDialog.findViewById(R.id.line2).setVisibility(View.VISIBLE);
			if(i == 3) mDialog.findViewById(R.id.line3).setVisibility(View.VISIBLE);
			if(i == 4) mDialog.findViewById(R.id.line4).setVisibility(View.VISIBLE);
			
			if(isClass(item.getInteger("id")))
				((TextView)mDialog.findViewById(R.id.filter0 + i)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win_focus, 0, 0, 0);
			else 
				((TextView)mDialog.findViewById(R.id.filter0 + i)).setCompoundDrawablesWithIntrinsicBounds(R.drawable.check_win, 0, 0, 0);

			String className = item.getString("class_name");
			if(item.getString("class_alias") != null && item.getString("class_alias").length() > 0) className = item.getString("class_alias");
			((TextView)mDialog.findViewById(R.id.filter0 + i)).setText(className);
		}

		mDialog.findViewById(R.id.confirm).setOnClickListener(listen);
		mDialog.findViewById(R.id.dialog_layout).setOnClickListener(listen);
		mDialog.show();
	}
	
	void removeClassId(Integer classId)
	{
		for(Integer id : ((DemoApp)mActivity.getApplicationContext()).mClassesSelected)
		{
			if(id.intValue() == classId.intValue())
			{
				((DemoApp)mActivity.getApplicationContext()).mClassesSelected.remove(id);
				break;
			}
		}
	}
	
	boolean isClass(Integer classId)
	{
		for(Integer id : ((DemoApp)mActivity.getApplicationContext()).mClassesSelected)
		{
			if(id.intValue() == classId.intValue())
			{
				return true;
			}
		}
		
		return false;
	}
	
	void showQuerySearch(final int postId) {
		final Dialog mDialog = new Dialog(mActivity, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_input);
		mDialog.findViewById(R.id.layout).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
					}
				});
		mDialog.findViewById(R.id.btn1).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String text = ((EditText)mDialog.findViewById(R.id.content)).getText().toString();
						if(text.length() == 0)
						{
							ToastUtil.showToastWithAlertPic("请输入内容");
							return;
						}
						
						sendCommentkRequest(postId,text);
						mDialog.dismiss();
					}
				});
		mDialog.show();
		
		((EditText)mDialog.findViewById(R.id.content)).setOnEditorActionListener(new OnEditorActionListener(){

			@Override
			public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
				return true;
			}
		}); 
		/*
		final View activityRootView = mDialog.findViewById(R.id.content);
		activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
			@Override
			public void onGlobalLayout(){
				int heightDiff = activityRootView.getRootView().getHeight()- activityRootView.getHeight();
				if(heightDiff >100){
					mDialog.findViewById(R.id.sep).setVisibility(View.VISIBLE);
				}
				else{
					mDialog.findViewById(R.id.sep).setVisibility(View.GONE);
				}
			}
		});*/
	}
	
	public void autoRefresh()
	{
		if(mAdapter == null || mArrayList == null) return;
		
		mArrayList.removeAll(mArrayList);
		mAdapter.notifyDataSetChanged();
		sendRequest(false);
	}
	
	private void sendRequest(boolean needAdd) {
//		if(needShowWait)
//		{
//			final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) mView.findViewById(R.id.listview);
//			ptrlvHeadlineNews.st.onRefreshComplete()
//		}

		mView.findViewById(R.id.tip).setVisibility(View.GONE);
		MgqDataHandler loginHandler = new MgqDataHandler(mContext, needShowWait, false) {
			@Override
			public void onSuccess(String response) {
				needShowWait = false;
				((PullToRefreshListView) mView.findViewById(R.id.listview)).onRefreshComplete();
				
				JSONObject ruser = JSON.parseObject(response);
				JSONArray member = ruser.getJSONArray("datalist");
				String timestamp = ruser.getString("timestamp");
				if(mType == 4) PreferencesUtils.putString(mContext, "sycn_timestamp",timestamp);
				
				mArrayList.removeAll(mArrayList);
				for(int i = 0; i < member.size(); i++)
				{
					JSONObject item = member.getJSONObject(i);
					
					StringBuffer praise = new StringBuffer();
					StringBuffer comments = new StringBuffer();
					
					JSONArray pArray = item.getJSONArray("praise");
					JSONArray cArray = item.getJSONArray("comment");
					
					for(int j = 0; pArray != null && j < pArray.size();j++)
					{
						if(j > 0) praise.append("，");//18073179777
						
						JSONObject comment = pArray.getJSONObject(j);
						String name = comment.getString("username");
						if(comment.getString("alias") != null && comment.getString("alias").length() != 0) name = comment.getString("alias");
						
						if(comment.getString("member_id").equals(PreferencesUtils.getString(mActivity, "userId"))) name = "我";
						praise.append(name);
					}
					
					for(int j = 0;cArray != null && j < cArray.size();j++)
					{
						JSONObject comment = cArray.getJSONObject(j);
						if(j > 0) comments.append("\n");
						
						String name = comment.getString("username");
						if(comment.getString("alias") != null && comment.getString("alias").length() != 0) name = comment.getString("alias");
						
						if(comment.getString("member_id").equals(PreferencesUtils.getString(mActivity, "userId"))) name = "我";
						comments.append(name + ": " + comment.getString("comment"));
					}
					
					item.put("praiseStr", praise.toString());
					item.put("commentStr", comments.toString());
					mArrayList.add(item);
				}
				
				((MainPageActivity)mActivity).setBadgeRequest(ruser.getString("badge"));
				mAdapter.notifyDataSetChanged();
				((PullToRefreshListView) mView.findViewById(R.id.listview)).onRefreshComplete();
				
				mView.findViewById(R.id.tip).setVisibility(View.GONE);
				if(mArrayList.size() == 0) mView.findViewById(R.id.tip).setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFailure(Throwable ble) {
				needShowWait = false;
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		List<Integer> list = ((DemoApp)mActivity.getApplicationContext()).mClassesSelected;
		if(mType == 1 || mType == 2 || mType == 5)
		{
			isExpite(list);
		}
		
		String ids = "";
		for(Integer id : list)
		{
			if(ids.length() > 0) ids += ",";
			ids += id;
		}
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(mActivity, "userId"));
		params.put("class_id", ids);
		params.put("type", mType);
		if(mType == 4) 
		{
			String timestamp = PreferencesUtils.getString(mContext, "sycn_timestamp");
			if(timestamp == null || timestamp.length() == 0) timestamp = "0";
			params.put("timestamp", timestamp);
		}
		
		params.put("start",0);
		if(needAdd) params.put("perpage", mArrayList.size() + 10);
		else params.put("perpage", 20);

		params.put("sec",Utils.Md5("get_post_listylbb"));
		MgqRestClient.get("get_post_list", params, loginHandler);
		
		if(warningStr != null && warningStr.length() > 0)
		{
        	ToastUtil.showToastWithAlertPic(warningStr);
			warningStr = "";
		}
	}
	
	public List<Integer> isExpite(List<Integer> ids)
	{
		JSONObject member = JSON.parseObject(PreferencesUtils.getString(mActivity, "member"));
		JSONArray childs = member.getJSONArray("children");
		JSONArray classes = member.getJSONArray("classes");
		
		String tip = "";
		List<Integer> res = new ArrayList<Integer>();
	    for(int i = 0; i < childs.size(); i++)
	    {
	    	JSONObject item = childs.getJSONObject(i);
	    	String expire_date = item.getString("expire_date");
	    	
	        if(isContain(ids, item.getIntValue("class_id")))
	        {
	            if(expire_date == null || expire_date.equals("0"))
	            {
	            	if(tip.length() > 0)tip += ",";
	            	tip += item.getString("name");
	            }
	            else
	            {
	            	Date date = Utils.ConverToDate(expire_date, "yyyy-MM-dd HH:mm:ss");
	                if(date.compareTo(new Date()) > 0)
	                {
	                    if(mType != 5) res.add(item.getInteger("class_id"));
	                    else res.add(item.getInteger("student_id"));

	                }
	                else
	                {
		            	if(tip.length() > 0)tip += ",";
		            	tip += item.getString("name");
	                }
	            }
	        }
	    }
	    
        if(warningStr == null && tip.length() > 0)
        {
        	warningStr = tip + "\n" + "服务已过期，请续费";
        }

	    if(mType != 5)
	    {
	        for(int i = 0; i < classes.size(); i++)
	        {
	        	JSONObject item = classes.getJSONObject(i);
	        	if(item.getInteger("is_teacher") > 0)
	        	{
	        		if(isContain(ids,item.getIntValue("id")) && !isContain(res,item.getIntValue("id")))
	        		{
	        			res.add(item.getInteger("id"));
	        		}
	        	}
	        }
	    }

	    return res;
	}
	
	boolean isContain(List<Integer> ids,int classId)
	{
	    for(Integer item : ids)
	    {
	        if(item.intValue() == classId)
	        {
	            return true;
	        }
	    }

	    return false;
	}
}